<?php
 
  class Commission_m extends CI_Model
  {
      
      function __construct()
      {
         parent::__construct();
         $this->load->database();
     }
        
     function insert($arr)
     {
         return $this->db->insert('commission',$arr);
     }  
     
     function update($where,$arr)
     {  
         $this->db->where($where);
         $data = $this->db->update('commission',$arr);
         return  $data;
     }
            
     function delete($where)
     {
         $this->db->where($where);
         $this->db->delete('commission');
     }
            
     
     //正序 全查         
     function select($where='',$order_by='',$limit='',$offset='')
     {  
         // where 条件
         if($where){
          $this->db->where($where);  
         }
         
         // order_by 条件
         if($order_by){
          $this->db->order_by($order_by);
         }
         // limit 条件
         if($offset&&$limit){
            $this->db->limit($limit,$offset);
         }
         if(!$offset&&$limit){
          $this->db->limit($limit);
         }
         
         $this->db->select('*');
         $data = $this->db->get('commission');
         return  $data->result_array();
     }

     //获取奖金总金额
     function getAllMoney($where){
        // where 条件
         if($where){
          $this->db->where($where);  
         }
        $this->db->select('*');
        $data = $this->db->get('commission');
        $data = $data->result_array();
        $money=0;
        if($data){
            foreach ($data as $key => $value) {
                $money+=$value['money'];
            }
        }
        return $money;
     }

     //获取佣金记录
     function getList($where,$page,$limit){
        // where 条件
         if($where){
          $this->db->where($where);  
         }
        $this->db->order_by('id desc');
        $offset=($page-1)*$limit;
        $this->db->limit($limit,$offset);
        $data = $this->db->get('commission');
        $data = $data->result_array();
        return $data;
     }

    //获取佣金记录
     function getmost($where){
        // where 条件
         if($where){
          $this->db->where($where);  
         }
        $data = $this->db->get('commission');
        $data = $data->result_array();
        $result['most']=0;
        $result['check']=0;
        if($data){
            foreach ($data as $key => $value) {
                if($value['settlement']==1){
                    $result['check']+=$value['money'];
                }
                $result['most']+=$value['money'];
            }
        }
        return $result;
     }

     function getLastCommission($device_sn){

          $this->db->where(array('device_sn'=>$device_sn));  
         
          $this->db->order_by('id desc');

          $this->db->limit('1');
         
         $this->db->select('addtime,flow');
         $data = $this->db->get('commission');
         $data=$data->result_array();
         if($data){
            $data=$data[0];
            $this->db->where(array('device_sn'=>$device_sn));
            $this->db->order_by('id asc');
            $this->db->limit('1');             
            $this->db->select('addtime,flow');
            $res = $this->db->get('commission');
            $res=$res->result_array();
            $data['addtime']=$res[0]['addtime'];
         }else{
            $data['flow']="未激活";
            $data['addtime']="未激活";
         }
         return  $data;
     }
     
 }
 ?>